// 封装模块————点菜
// ***************
    // 核心 做菜顺序队列
    var allfood = []
// ***************
let od = getPageInfo(),
    food_order = document.getElementsByClassName('food')[0],
    main_page = document.getElementsByClassName('main_page')[0],
    food_header = document.getElementsByClassName('food_header')[0], // 顶部信息
    food_main = document.getElementsByClassName('food_main')[0], // 点菜信息
    food_foot = document.getElementsByClassName('food_foot')[0] // 底部按钮

let seat_color = ['red','green','blue','orange']
// 将选择食物铺满界面
food_order.style.width = `${od.width}px`
food_order.style.height = `${od.height}px`

// 给main_page设置宽高
main_page.style.width = `${od.width * 0.9}px`
main_page.style.height = `${od.height * 0.7}px`

// 给food_header设置宽高
food_header.style.width = `${od.width * 0.9}px`
food_header.style.height = `${od.height * 0.05}px`
food_header.style.lineHeight = `${od.height * 0.05}px`


// 给food_main设置宽高
food_main.style.width = `${od.width * 0.85}px`
food_main.style.height = `${od.height * 0.5}px`

// 给food_main中的元素设置自适应行高
for (let i = 0; i < food_main.children.length; i++) {
    food_main.children[i].style.lineHeight = parseInt(food_main.style.height) / 13 + 'px'
}


// 给food_foot设置宽高
food_foot.style.width = `${od.width * 0.8}px`
food_foot.style.height = `${od.height * 0.1}px`


function start_order(obj,seat,ii) {
    // 顾客耐心值  暂未完善
    // clearTimeout(custom_waittime[0])
    // console.log(custom_waittime)
    // custom_waittime.shift()
    food_order.style.display = 'block'
    var ok = document.getElementsByClassName('btn_yes')[0],
        no = document.getElementsByClassName('btn_no')[0]

    document.getElementsByClassName('food_header')[0].children[1].children[0].innerHTML = obj.name

    // 将用户头像放入
        document.getElementsByClassName('food_header')[0].children[0].style.backgroundImage = obj.touxiang
    // *************

    // 在此清除选中的信息并且给每一个选择框设置一个onclick金额动态变化
    var main = [...document.getElementsByClassName('food_main')[0].children],
        money = document.getElementsByClassName('food_header')[0].children[1].children[1],
        all = [0, 0, 0], cold = [], drink = []
    
    cold.push(main.shift()); cold.push(main.shift());cold.push(main.shift());
    drink.push(main.pop()); drink.push(main.pop());drink.push(main.pop());

    money.innerHTML = '0' // 清空上一轮残留的金额

    // 给每一个条目设置一个点击事件
    for (let i = 0; i < cold.length; i++) {
        if (cold[i].children.length !== 0) {
            cold[i].children[0].children[0].checked = false
            cold[i].onclick = function () {
                money.innerHTML = parseInt(cold[i].children[0].children[2].innerHTML) + parseInt(money.innerHTML) - all[0]
                all[0] = parseInt(cold[i].children[0].children[2].innerHTML)
            }
        }
    }

    for (let i = 0; i < main.length; i++) {
        if (main[i].children.length !== 0) {
            main[i].children[0].children[0].checked = false
            main[i].onclick = function () {
                money.innerHTML = parseInt(main[i].children[0].children[2].innerHTML) + parseInt(money.innerHTML) - all[1]
                all[1] = parseInt(main[i].children[0].children[2].innerHTML)

            }
        }
    }

    for (let i = 0; i < drink.length; i++) {
        if (drink[i].children.length !== 0) {
            drink[i].children[0].children[0].checked = false
            drink[i].onclick = function () {
                money.innerHTML = parseInt(drink[i].children[0].children[2].innerHTML) + parseInt(money.innerHTML) - all[2]
                all[2] = parseInt(drink[i].children[0].children[2].innerHTML)
            }
        }
    }
    // 事件设置完毕
    // ******************
    // 给最下方按钮设置点击事件
    ok.onclick = function () {
        // 此处需要收集信息公用
        // 首先判断必选菜是否选择
        var main = [...document.getElementsByClassName('food_main')[0].children]
        main.shift()
        var cold = []
        cold.push(main.shift())
        cold.push(main.shift())
        main.shift()
        var drink = []
        drink.push(main.pop())
        drink.push(main.pop())
        main.pop();
        for (let j = 0; j < cold.length; j++) {
            if (cold[j].children[0].children[0].checked) {
                let food_obj = {
                    // 食物队列对象
                    food_name:'',
                    money:0,
                    user_name:''
                } 
                obj.food.push(cold[j].children[0].children[1].innerHTML)
                food_obj.food_name = cold[j].children[0].children[1].innerHTML
                food_obj.money = parseInt(cold[j].children[0].children[2].innerHTML)
                food_obj.user_name = obj.name                
                allfood.push(food_obj)
                
                obj.money.push(parseInt(cold[j].children[0].children[2].innerHTML))
            }
        }
        for (let j = 0; j < drink.length; j++) {
            if (drink[j].children[0].children[0].checked) {
                let food_obj = {
                    // 食物队列对象
                    food_name:'',
                    money:0,
                    user_name:''
                } 
                obj.food.push(drink[j].children[0].children[1].innerHTML)
                obj.money.push(parseInt(drink[j].children[0].children[2].innerHTML))
                
                food_obj.food_name = drink[j].children[0].children[1].innerHTML
                food_obj.money = parseInt(drink[j].children[0].children[2].innerHTML)
                food_obj.user_name = obj.name                
                allfood.push(food_obj)
            }
        }
        for (let i = 0; i < main.length; i++) {
            if (main[i].children[0].children[0].checked) {
                let food_obj = {
                    // 食物队列对象
                    food_name:'',
                    money:0,
                    user_name:''
                } 
                obj.food.push(main[i].children[0].children[1].innerHTML)
                obj.money.push(parseInt(main[i].children[0].children[2].innerHTML))

                food_order.style.display = 'none'
                food_obj.food_name = main[i].children[0].children[1].innerHTML
                food_obj.money = parseInt(main[i].children[0].children[2].innerHTML)
                food_obj.user_name = obj.name                
                allfood.push(food_obj)

                // console.log(obj) // 用户信息收集完毕
                // console.log(allfood)

                // 信息收集完成后添加至座位信息
                seat.obj.touxiang = obj.touxiang
                seat.style.backgroundImage = seat.obj.touxiang
                seat.style.backgroundColor = seat_color[Math.floor(Math.random()*4)]
                seat.userinfo = obj
                // console.log(seat)
                
                // 在这里触发判断该用户点菜情况并为其添加相应的侧边栏
                for(let i = 0 ; i < obj.food.length ; i++){
                    table_food(ii,obj.food[i])
                }

                // 移除用户队列的信息
                let cus = document.getElementsByClassName('custom_in')[0]
                line.removeChild(cus)
                
                S_Tips(`顾客${obj.name}已点餐`)
                break;
            } else if (i === main.length - 1) {
                S_Tips('请选择主食!')
            }
        }        
    }

    no.onclick = function () {
        // 此处需要将用户信息清除
        let cus = document.getElementsByClassName('custom_in')[0]
        line.removeChild(cus)
        alluser.shift() // 移走用户
        food_order.style.display = 'none'
    }
    // 事件设置完毕
    // ***********
}       